package com.healthai.service;

import com.healthai.entity.DailyRecord;
import com.healthai.entity.ExerciseRecord;
import com.healthai.entity.DietRecord;
import com.healthai.exception.BusinessException;
import com.healthai.util.AiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HealthService {
    
    @Autowired
    private DailyRecordService dailyRecordService;
    
    @Autowired
    private ExerciseRecordService exerciseRecordService;
    
    @Autowired
    private DietRecordService dietRecordService;
    
    @Autowired
    private AiUtil aiUtil;
    
    /**
     * 获取健康建议
     * @param userId 用户ID
     * @return 健康建议
     */
    public String getHealthAdvice(Integer userId) {
        // 获取用户最近的每日记录
        List<DailyRecord> dailyRecords = dailyRecordService.getRecentDailyRecords(userId, 7);
        if (dailyRecords.isEmpty()) {
            throw new BusinessException(404, "未找到健康记录，无法生成建议");
        }
        
        // 获取用户最近的运动记录
        List<ExerciseRecord> exerciseRecords = exerciseRecordService.getRecentExerciseRecords(userId, 7);
        
        // 获取用户最近的饮食记录
        List<DietRecord> dietRecords = dietRecordService.getRecentDietRecords(userId, 7);
        
        // 构建健康数据
        Map<String, Object> healthData = new HashMap<>();
        healthData.put("dailyRecords", dailyRecords);
        healthData.put("exerciseRecords", exerciseRecords);
        healthData.put("dietRecords", dietRecords);
        
        // 调用AI获取健康建议
//        return aiUtil.getHealthAdvice(healthData);
        return "健康建议";
    }
    
    /**
     * 获取健康报告
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 健康报告
     */
    public Map<String, Object> getHealthReport(Integer userId, LocalDate startDate, LocalDate endDate) {
        // 获取日期范围内的每日记录
        List<DailyRecord> dailyRecords = dailyRecordService.getDailyRecordsByDateRange(userId, startDate, endDate);
        // 获取日期范围内的运动记录
        List<ExerciseRecord> exerciseRecords = exerciseRecordService.getExerciseRecordsByDateRange(userId, startDate, endDate);
        // 获取日期范围内的饮食记录
        List<DietRecord> dietRecords = dietRecordService.getDietRecordsByDateRange(userId, startDate, endDate);

        // 构建健康报告
        Map<String, Object> healthReport = new HashMap<>();
        healthReport.put("startDate", startDate);
        healthReport.put("endDate", endDate);
        healthReport.put("dailyRecords", dailyRecords);
        healthReport.put("exerciseRecords", exerciseRecords);
        healthReport.put("dietRecords", dietRecords);
        return healthReport;
    }

}